
<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行', '新款', '精选']"
                 @tabClick="tabClick"
                 ref="tabControl1"
                 v-show="isTabFixed"/>
    <back-top @click.native="backClick" v-show="showBack"/>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend :recommends="recomends"/>
      <feature-view/>
      <tab-control :titles="['流行', '新款', '精选']"
                   @tabClick="tabClick"
                   ref="tabControl"/>
      <good-list :goods="showGoods"/>
    </scroll>
  </div>
</template>

<script>
  import NavBar from "../../components/common/navbar/NavBar";
  import Recommend from "./childrenComps/Recommend";
  import HomeSwiper from "./childrenComps/HomeSwiper";
  import FeatureView from "./childrenComps/FeatureView";
  import GoodList from "../../components/content/goods/GoodList";

  import TabControl from "../../components/content/tabControl/TabControl";
  import Scroll from "../../components/common/scroll/Scroll";
  import BackTop from "../../components/content/BockTop/BackTop";

  import {getHomeMultidate, getHomeGoods} from "../../network/home";
  import {debounce} from "../../common/uni";
  import {itemListenerMixin} from "../../common/mixin";

  export default {
    name: "Home",
    data() {
      return {
        banners: [],
        recomends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []},
        },
        cType: 'pop',
        showBack: false,
        taboffsetTop: 0,
        isTabFixed: false,


      };
    },
    mixins:[itemListenerMixin],
    computed: {
      showGoods() {
        return this.goods[this.cType].list
      }
    },
    components: {
      NavBar,
      HomeSwiper,
      Recommend,
      FeatureView,
      TabControl,
      GoodList,
      Scroll,
      BackTop,

    },

    created() {
      getHomeMultidate().then(res => {
        this.banners = res.data.banner.list;
        this.recomends = res.data.recommend.list;
      })
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')
    },
    mounted() {

    },
    deactivated() {
      this.$bus.$off('itemImageLoad',this.itemImgListener)
    },
    methods: {
      swiperImageLoad() {
        this.taboffsetTop = this.$refs.tabControl.$el.offsetTop
        // console.log(this.taboffsetTop)
      },

      backClick() {
        this.$refs.scroll.scrollTo(0, 0, 200)
      },
      tabClick(index) {
        if (index === 0) {
          this.cType = 'pop'
        } else if (index === 1) {
          this.cType = 'new'
        } else {
          this.cType = 'sell'
        }
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl.currentIndex = index
      },
      contentScroll(position) {
        // console.log(position)
        this.showBack = (-position.y) > 1000
        this.isTabFixed = (-position.y) > this.taboffsetTop
      },
      loadMore() {
        this.getHomeGoods(this.cType)
      },
      getHomeGoods(type) {
        let page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list)
          this.$refs.scroll.finishPullUp()
        })
      }
    }
  };
</script>
<style scoped>
  #home {
    /*padding-top: 44px;*/
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;

    /*在使用浏览器原生滚动时, 为了让导航不跟随一起滚动*/
    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 9;*/
  }

  .fixed {
    position: fixed;
    left: 0;
    right: 0;
    top: 40px;
  }

  .content {
    /*height: 500px;*/
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }


</style>